// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import './assets/less/public.less'
import {
  localData
} from "./assets/js/local"
import plugin from './components/'

import axios from 'axios'
axios.defaults.withCredentials = true; //让ajax请求头部携带cookie


// 引入element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


Vue.use(ElementUI)
Vue.use(plugin)


//贷超后台管理系统 -- 正式
// Vue.prototype.host = '';

//贷超后台管理系统 -- 测试
// Vue.prototype.host = 'http://192.168.3.55:10031';

Vue.prototype.host = '/api/';


//localStorage操作
Vue.prototype.localData = localData;

//封装ajax
Vue.prototype.ajaxJson = function (options) {
  let that = this;

  if (options.data) {

  }

  axios({
    url: that.host + options.url,
    method: 'POST',
    headers: options.headers,
    data: options.data,
  }).then(function (res) {
    if (res.status == 200) {
      options.success(res.data);
      let code = res.data.code;
      if (code != '0') {
        that.$message.error(res.data.message);
        if (code == '1003') {
          that.logout()
        }
      }
    }
  }).catch(function (error) {
    if (error.response) {
      let code = error.response.status;
      if (code == '404') {
        that.$message.error('接口不存在');
        options.success({})
      } else {
        that.$message.error('服务器错误');
        options.success({})
      }
    }
  })
}

//退出请求
Vue.prototype.logout = function () {
  let that = this;
  that.ajaxJson({
    url: 'auth/logout',
    success: function (data) {
      that.localData('clean', 'login_user');
      that.$router.push({
        name: 'login'
      });
    }
  })
}


//报错信息
Vue.prototype.showMsg = function (val, status) {
  let that = this;
  that.$message({
    message: val,
    type: status
  });
}

//表格显示时间
Vue.prototype.formatter_time = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd') : ''
}
Vue.prototype.formatter_timeYMDHMS = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd hh:mm:ss') : ''
}
Vue.prototype.formatter_timeYMDHM = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd hh:mm') : ''
}

//表格显示产品状态
Vue.prototype.formatter_productStatus = function (row, column, cellValue, index) {
  switch(cellValue){
    case 0:
      return "推广中";
    case 1:
      return "已下架";
  }
}

//表格显示产品上下架操作
Vue.prototype.formatter_productHandel = function (row, column, cellValue, index) {
  switch(cellValue){
    case 0:
      return "下架";
    case 1:
      return "上架";
  }
}

//时间选择器限制当前时间
Vue.prototype.pickerOptions = {
  disabledDate(time) {
    return time.getTime() > Date.now();
  }
}

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: {
    App
  },
  template: '<App/>'
})


// oss参数测试
Vue.prototype.OssParameter = {
  accessKeyId: 'LTAI83HVuUfaB4UI',
  accessKeySecret: 'Z7ucFCs4Z4oTjuHrEbbWex1Jj8yyrY',
  region: 'oss-cn-hangzhou',
  bucket: 'guoyuechuzu',
  folder: 'product_picture/'
};

Vue.prototype.uploadFile = function (options) {
let that = this;
if(!options.file){
  return;
}
let fileName = options.file.name;
let filelastName = fileName.substring(fileName.lastIndexOf(".") + 1)
let ossParameter = that.OssParameter;
let client = new OSS.Wrapper({
  secure: true,
  accessKeyId: ossParameter.accessKeyId,
  accessKeySecret: ossParameter.accessKeySecret,
  region: ossParameter.region,
  bucket: ossParameter.bucket,
});
let path = ossParameter.folder  + (new Date()).getTime() + parseInt((Math.random()*100000 + 0),10) + '.'+ filelastName;
client.multipartUpload(path, options.file).then(function (res) {
  let href = 'https://' + ossParameter.bucket + '.' + ossParameter.region + '.aliyuncs.com/' + res.name;
  options.success(href)
}).catch(function (err) {
});
}



Date.prototype.Format = function (fmt) {
  let o = {
    "M+": this.getMonth() + 1,         // 月份
    "d+": this.getDate(),          // 日
    "h+": this.getHours(),          // 小时
    "m+": this.getMinutes(),         // 分
    "s+": this.getSeconds(),         // 秒
    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
    "S": this.getMilliseconds()       // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
};
